<?php
namespace App\Libraries\Tools;


use App\Models\Post\PostComment;
use App\Models\Shop\ShopCouponPurchase;
use App\Models\User\UserPoint;
use App\Models\User\UserPointLog;
use Carbon\Carbon;

class Point
{

    private $max_point = 40;
    private $post_point = 5;
    private $comment_point = 1;

    /**
     * 添加发帖积分
     * @param $user_id
     * @param $post_id
     * @return bool
     */
    public function addPostPoint($user_id, $post_id){
        if(!CheckAuth::checkIfValidUser($user_id)){
            return false;
        }
        $real_point = $this->post_point;
        $real_comment = Msg::point_post();
        $real_status = ConstVar::$POINT_STATUS_WAITING;
        if($this->checkIfBeyondMaxPoint($user_id, $this->post_point)){
            $real_point = 0;
            $real_comment = Msg::point_max();
            $real_status = ConstVar::$POINT_STATUS_SUCCESS;
        }
        UserPointLog::create([
            'reason' => ConstVar::$POINT_REASON_POST,
            'comment' => $real_comment,
            'user_id' => $user_id,
            'origin_id' => $post_id,
            'origin_type' => 1,
            'status' => $real_status,
            'point' => $real_point,
        ]);
        return true;
    }

    /**
     * 添加回复积分
     * @param $user_id
     * @param $post_id
     * @param $post_user_id
     * @param $comment_id
     * @return bool
     */
    public function addCommentPoint($user_id, $post_id, $post_user_id, $comment_id){
        if(!CheckAuth::checkIfValidUser($user_id)){
            return false;
        }
        $real_point = $this->comment_point;
        $real_comment = Msg::point_comment();
        $real_status = ConstVar::$POINT_STATUS_WAITING;
        if(PostComment::where('user_id', $user_id)->where('post_id', $post_id)->first()){
            $real_point = 0;
            $real_comment = Msg::point_comment_repeat();
            $real_status = ConstVar::$POINT_STATUS_SUCCESS;
        }
        if($user_id == $post_user_id){
            $real_point = 0;
            $real_comment = Msg::point_comment_self();
            $real_status = ConstVar::$POINT_STATUS_SUCCESS;
        }
        if($this->checkIfBeyondMaxPoint($user_id, $this->comment_point)){
            $real_point = 0;
            $real_comment = Msg::point_max();
            $real_status = ConstVar::$POINT_STATUS_SUCCESS;
        }
        UserPointLog::create([
            'reason' => ConstVar::$POINT_REASON_COMMENT,
            'comment' => $real_comment,
            'user_id' => $user_id,
            'origin_id' => $comment_id,
            'origin_type' => 2,
            'status' => $real_status,
            'point' => $real_point,
        ]);

        $comment_count = PostComment::where('post_id', $post_id)
            ->where('status', 1)
            ->groupBy('user_id')
            ->count();
        if($comment_count >= 51){
            $this->addCommentedPoint($post_user_id, $post_id, 20);
        }elseif($comment_count >= 21){
            $this->addCommentedPoint($post_user_id, $post_id, 10);
        }elseif($comment_count >= 11){
            $this->addCommentedPoint($post_user_id, $post_id, 5);
        }elseif($comment_count >= 6){
            $this->addCommentedPoint($post_user_id, $post_id, 2);
        }
        return true;
    }

    /**
     * 添加消费套餐积分
     * @param ShopCouponPurchase $shopCouponPurchase
     * @return bool
     */
    public function addCouponPoint(ShopCouponPurchase $shopCouponPurchase){
        if(!CheckAuth::checkIfValidUser($shopCouponPurchase->user_id)){
            return false;
        }
        UserPointLog::create([
            'reason' => ConstVar::$POINT_REASON_CONSUMPTION,
            'comment' => Msg::point_coupon(),
            'user_id' => $shopCouponPurchase->user_id,
            'origin_id' => $shopCouponPurchase->id,
            'origin_type' => 3,
            'status' => ConstVar::$POINT_STATUS_SUCCESS,
            'point' => ceil($shopCouponPurchase->amount),
        ]);
        $this->resum($shopCouponPurchase->user_id);
        return true;
    }

    /**
     * 检查是否超出每天限额
     * @param $user_id
     * @param $add_point
     * @return bool
     */
    public function checkIfBeyondMaxPoint($user_id, $add_point){
        $today_start = Carbon::today()->timestamp;
        $today_end = $today_start + 86399;

        $today_points = UserPointLog::where('user_id', $user_id)->where('point', '>', 0)
            ->whereBetween('created_at', [$today_start, $today_end])->sum('point');

        if($today_points + $add_point > $this->max_point){
            return true;
        }
        return false;
    }

    /**
     * 添加被回复积分
     * @param $post_user_id
     * @param $post_id
     * @param $add_point
     * @return bool
     */
    private function addCommentedPoint($post_user_id, $post_id, $add_point){
        if(!CheckAuth::checkIfValidUser($post_user_id)){
            return false;
        }
        $log = UserPointLog::where('user_id', $post_user_id)->where('origin_id', $post_id)
            ->where('origin_type', 2)
            ->where('reason', ConstVar::$POINT_REASON_COMMENTED)
            ->whereIN('status', [ConstVar::$POINT_STATUS_WAITING,ConstVar::$POINT_STATUS_SUCCESS])
            ->where('point', $add_point)->first();
        if(empty($log)){
            UserPointLog::create([
                'reason' => ConstVar::$POINT_REASON_COMMENTED,
                'comment' => Msg::point_commented(),
                'user_id' => $post_user_id,
                'origin_id' => $post_id,
                'origin_type' => 1,
                'status' => ConstVar::$POINT_STATUS_WAITING,
                'point' => $add_point,
            ]);
        }
        return true;
    }

    /**
     * 重新计算某用户的积分
     * @param $user_id
     */
    public function resum($user_id){
        $log = new UserPointLog();
        $user_point = new UserPoint();
        $log->where('user_id', $user_id)->where('status', 1)->update(['status'=>3]);
        $points = $log->where('user_id', $user_id)
            ->where('status', 3)
            ->sum('point');
        $user_point->updateOrCreate(['user_id'=>$user_id], ['point'=>$points, 'total_point'=>$points]);
    }

    /**
     * 改变某条流水记录的状态
     * @param $user_id
     * @param $origin_id
     * @param $origin_type
     * @param $status
     */
    public function setStatus($user_id, $origin_id, $origin_type, $status){
        UserPointLog::where('user_id', $user_id)
            ->where('origin_id', $origin_id)
            ->where('origin_type', $origin_type)
            ->update(['status'=>$status]);
    }

    /**
     * 根据积分返回等级
     * @param $point
     * @return int
     */
    public static function getLevel($point){
        if($point >= 30001){
            return 5;
        }elseif($point >= 5001){
            return 4;
        }elseif($point >= 1001){
            return 3;
        }elseif($point >= 101){
            return 2;
        }else{
            return 1;
        }
    }

    public static function pointToNextLevel($point){
        if($point >= 30001){
            return 0;
        }elseif($point >= 5001){
            return 30001 - $point;
        }elseif($point >= 1001){
            return 5001 - $point;
        }elseif($point >= 101){
            return 1001 - $point;
        }else{
            return 101 - $point;
        }
    }
}